package cn.xxstudy.permission.help

import android.content.Intent
import android.content.pm.PackageManager
import androidx.fragment.app.Fragment

/**
 * @author ：wuxinxi on 2020/11/10 .
 * @packages ：cn.xxstudy.permission.help .
 * TODO:对外隐藏的处理权限的Fragment
 */
class PermissionFragment : Fragment() {
    lateinit var permissionBuilder: PermissionBuilder

    companion object {
        const val REQUEST_NORMAL_PERMISSION_CODE = 1
        const val REQUEST_SETTINGS_CODE = 2
    }

    fun request(permissionBuilder: PermissionBuilder, permissions: Array<out String>) {
        this.permissionBuilder = permissionBuilder
        requestPermissions(permissions, REQUEST_NORMAL_PERMISSION_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_SETTINGS_CODE) {
            if (isNull()) {
                requestPermissions(permissionBuilder.permissions, REQUEST_NORMAL_PERMISSION_CODE)
            }
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_NORMAL_PERMISSION_CODE) {
            if (isNull()) {
                onRequestNormalPermissionsResult(permissions, grantResults)
            }
        }
    }

    private fun onRequestNormalPermissionsResult(permissions: Array<out String>, grantResults: IntArray) {
        permissionBuilder.grantedPermissions.clear()
        permissionBuilder.deniedPermissions.clear()
        permissionBuilder.permanentDeniedPermissions.clear()
        for ((index, item) in permissions.withIndex()) {
            if (grantResults[index] == PackageManager.PERMISSION_GRANTED) {
                permissionBuilder.grantedPermissions.add(item)
                permissionBuilder.deniedPermissions.remove(item)
                permissionBuilder.permanentDeniedPermissions.remove(item)
            } else {
                val isNotPermanentDenied = shouldShowRequestPermissionRationale(item)
                if (isNotPermanentDenied) {
                    permissionBuilder.deniedPermissions.add(item)
                } else {
                    //勾选了不再提示
                    permissionBuilder.permanentDeniedPermissions.add(item)
                    permissionBuilder.deniedPermissions.remove(item)
                }
            }
        }

        if (permissionBuilder.deniedPermissionRetry &&
            permissionBuilder.deniedPermissions.size != 0
        ) {
            if (permissionBuilder.deniedPermissionRetry) {
                permissionBuilder.deniedPermissionRequestState = RequestState.REQUESTING
                permissionBuilder.retryAgain()
            }
        } else {
            permissionBuilder.deniedPermissionRequestState = RequestState.NONE
        }

        if (permissionBuilder.permanentDeniedPermissionToSettings
            && permissionBuilder.permanentDeniedPermissions.size != 0
        ) {
            if (permissionBuilder.deniedPermissionRequestState != RequestState.REQUESTING) {
                permissionBuilder.permanentDeniedPermissionRequestState = RequestState.REQUESTING
                permissionBuilder.showPermanentDeniedPermissionsDialog()
            }
        } else {
            permissionBuilder.permanentDeniedPermissionRequestState = RequestState.NONE
        }

        if (permissionBuilder.deniedPermissionRequestState != RequestState.REQUESTING &&
            permissionBuilder.permanentDeniedPermissionRequestState != RequestState.REQUESTING
        ) {
            permissionBuilder.callback(
                permissionBuilder.deniedPermissions.size == 0 && permissionBuilder.permanentDeniedPermissions.size == 0,
                permissionBuilder.grantedPermissions.toTypedArray(),
                permissionBuilder.deniedPermissions.toTypedArray(),
                permissionBuilder.permanentDeniedPermissions.toTypedArray()
            )
        }
    }

    private fun isNull(): Boolean {
        return this::permissionBuilder.isInitialized
    }
}